package com.TeacherInfoServer.common.service.Impl;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.pojo.DTO.AddTemporaryPositionDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.TemporaryPositionSearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateTemporaryPositionDTO;
import com.TeacherInfoServer.pojo.entity.TemporaryPosition;
import com.TeacherInfoServer.pojo.vo.TemporaryPositionListVo;
import com.TeacherInfoServer.pojo.vo.TemporaryPositionVo;
import com.TeacherInfoServer.exception.JobNumberNotExistException;
import com.TeacherInfoServer.mapper.TemporaryPositionMapper;
import com.TeacherInfoServer.common.service.TeacherService;
import com.TeacherInfoServer.common.service.TemporaryPositionService;
import com.TeacherInfoServer.utils.HttpServletUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TemporaryPositionServiceImpl extends ServiceImpl<TemporaryPositionMapper, TemporaryPosition> implements TemporaryPositionService {

    @Autowired
    private TemporaryPositionMapper temporaryPositionMapper;
    @Autowired
    private TeacherService teacherService;

    @Override
    @Transactional
    public Result<Map<String, Integer>> addOne(AddTemporaryPositionDTO addTemporaryPositionDTO) {
        if (teacherService.getOne(addTemporaryPositionDTO.getTeacherId()).getData() != null){
            TemporaryPosition temporaryPosition =
                    new TemporaryPosition(null,
                            addTemporaryPositionDTO.getTeacherId(),
                            addTemporaryPositionDTO.getUnit(),
                            addTemporaryPositionDTO.getDescription(),
                            addTemporaryPositionDTO.getStartDate(),
                            addTemporaryPositionDTO.getEndDate(),1,null);
            Integer record = temporaryPositionMapper.insert(temporaryPosition);
            Map<String,Integer> map = new HashMap<>();
            map.put("record",record);
            return Result.success(map);
        } else {
            throw new JobNumberNotExistException();
        }
    }

    @Override
    @Transactional
    public Result<TemporaryPositionListVo> getList(Long pageNumber, Long pageSize) {
        Page<TemporaryPosition> page = new Page<>(pageNumber,pageSize);
        List<TemporaryPosition> temporaryPositionList = temporaryPositionMapper.selectPageWithTeacher(page);
        List<TemporaryPositionVo> temporaryPositionVoList = getTemporaryPositionVoList(temporaryPositionList);
        TemporaryPositionListVo temporaryPositionListVo =
                new TemporaryPositionListVo(page.getCurrent(),
                        page.getSize(),
                        page.getPages(),
                        page.getTotal(),
                        temporaryPositionVoList);
        return Result.success(temporaryPositionListVo);
    }

    @Override
    public Map<String,Object> searchTemporaryPositionList(TemporaryPositionSearchDTO temporaryPositionSearchDTO) {
        Page<?> page = new Page<>(temporaryPositionSearchDTO.getPages(), temporaryPositionSearchDTO.getSize());
        temporaryPositionMapper.searchTemporaryPositionList(page, temporaryPositionSearchDTO);
        Map<String, Object> data = new HashMap<>();
        data.put("totals", page.getTotal());
        data.put("currentPages", page.getCurrent());
        data.put("pageNumber", page.getPages());
        data.put("rows", page.getRecords());
        return data;
    }

    @Override
    public void export(HttpServletResponse response, String fileName) throws IOException {
        HttpServletUtil.setExportResponse(response,fileName);
        List<TemporaryPosition> temporaryPositionList = temporaryPositionMapper.selectPageWithTeacher(null);
        List<TemporaryPositionVo> temporaryPositionVoList = getTemporaryPositionVoList(temporaryPositionList);
        EasyExcel.write(response.getOutputStream(), TemporaryPositionVo.class).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(temporaryPositionVoList);
    }


//    @Override
//    @Transactional
//    public Result<TemporaryPositionListVo> getOneList(Long pageNumber, Long pageSize, String teacherId) {
//        Page<TemporaryPosition> page = new Page<>(pageNumber,pageSize);
//        LambdaQueryWrapper<TemporaryPosition> wrapper = new LambdaQueryWrapper<>();
//        List<TemporaryPosition> temporaryPositionList = temporaryPositionMapper.selectPage(page,wrapper.eq(TemporaryPosition::getTeacherId,teacherId).orderByDesc(TemporaryPosition::getEndDate)).getRecords();
//        TemporaryPositionListVo temporaryPositionListVo =
//                new TemporaryPositionListVo(page.getCurrent(),
//                        page.getSize(),
//                        page.getPages(),
//                        page.getTotal(),
//                        temporaryPositionList);
//        return Result.success(temporaryPositionListVo);
//    }

    @Override
    @Transactional
    public Result<Map<String, Integer>> deleteBatch(Integer[] ids) {
        Integer record = temporaryPositionMapper.deleteBatchIds(Arrays.asList(ids));
        Map<String, Integer> map = new HashMap<>();
        map.put("record", record);
        return Result.success(map);
    }

    @Override
    public Result<Map<String, Integer>> updateOne(UpdateTemporaryPositionDTO updateTemporaryPositionDTO) {
        if (teacherService.getOne(updateTemporaryPositionDTO.getTeacherId()).getData() != null){
            LambdaUpdateWrapper<TemporaryPosition> wrapper = new LambdaUpdateWrapper<>();
            Integer record = temporaryPositionMapper.update(null,wrapper.eq(TemporaryPosition::getId, updateTemporaryPositionDTO.getId())
                    .set(TemporaryPosition::getTeacherId, updateTemporaryPositionDTO.getTeacherId())
                    .set(TemporaryPosition::getUnit, updateTemporaryPositionDTO.getUnit())
                    .set(TemporaryPosition::getDescription, updateTemporaryPositionDTO.getDescription())
                    .set(TemporaryPosition::getStartDate, updateTemporaryPositionDTO.getStartDate())
                    .set(TemporaryPosition::getEndDate, updateTemporaryPositionDTO.getEndDate()));
            Map<String, Integer> map = new HashMap<>();
            map.put("record", record);
            return Result.success(map);
        } else {
            throw new JobNumberNotExistException();
        }
    }

    private List<TemporaryPositionVo> getTemporaryPositionVoList(List<TemporaryPosition> temporaryPositionList){
        return temporaryPositionList.stream()
                .map(temporaryPosition -> new TemporaryPositionVo(
                        temporaryPosition.getId(),
                        temporaryPosition.getTeacherId(),
                        temporaryPosition.getTeacher().getTeacherName(),
                        temporaryPosition.getUnit(),
                        temporaryPosition.getDescription(),
                        temporaryPosition.getStartDate(),
                        temporaryPosition.getEndDate()
                ))
                .collect(Collectors.toList());
    }

}
